//
//  DINGNetTool.swift
//  0730Microblog
//
//  Created by ding on 15/7/31.
//  Copyright © 2015年 ding. All rights reserved.
//

import UIKit
import AFNetworking

private let DCZErrorDomainName = "com.dingcaozhi.heima"

class DINGNetTool: AFHTTPSessionManager {
    typealias DCZFinishedCallBack = (result: [String : AnyObject]?, error: NSError?)->()
    
    // 请求授权需要的参数
    private let clientID_appKey = "3080450150"
    let redirectUri = "http://www.sina.com.cn"
    // 获取授权需要的参数
    private let clientSecret_appSecret = "2e26490aae6d4b8ae7384655d74cdffc"
    
    // MARK: - 单例
    static let sharedNetTool: DINGNetTool = {
        let baseURL = NSURL(string: "https://api.weibo.com/")!
        let netTool = DINGNetTool(baseURL: baseURL)
        netTool.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json", "text/json", "text/javascript", "text/plain") as Set<NSObject>
        return netTool
    }()
    
    // MARK: - 返回授权地址URL
    func getOAuthAddress() -> NSURL {
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(clientID_appKey)&redirect_uri=\(redirectUri)"
        return NSURL(string: urlString)!
    }
    
    func loadUser(uid: String, finished: DCZFinishedCallBack) {
        if UserAcountInfo.loadSharedUserAccountInfo()?.access_token == nil {
            print("dingcaozhi----------accessToken 为空")
            return
        }
        let urlString = "2/users/show.json"
        let params : [String : AnyObject] = ["access_token" : UserAcountInfo.loadSharedUserAccountInfo()!.access_token!, "uid" : uid]
        requestGET(urlString, params: params, finished: finished)
    }
    
    
    /// 发送POST请求 拿到 Token相关信息
    func loadAccessToken(code: String, finished: DCZFinishedCallBack) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id": clientID_appKey,
            "client_secret": clientSecret_appSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirectUri]
        
        POST(urlString, parameters: params, success: { (_, JSON) -> Void in
            
            finished(result: JSON as? [String: AnyObject], error: nil)
            }) { (_, error) -> Void in
                print(error)
                finished(result: nil, error: error)
        }
    }
    
/// 封装AFNetworking的GET方法
    func requestGET(urlString: String, params: [String : AnyObject], finished:(result: [String : AnyObject]?, error: NSError?)->()) {
        
        GET(urlString, parameters: params, success: { (_, JSON) -> Void in
            if let result = JSON as? [String : AnyObject] {
                finished(result: result, error: nil)
            } else {
                // 没有数据同时没有结果
                let error = NSError(domain: DCZErrorDomainName, code: -100, userInfo: ["errorMessage" : "空数据"])
                finished(result: nil, error: error)
            }
            
            }) { (_, error) -> Void in
                finished(result: nil, error: error)
        }
    }
    
    /// 根据access_token加载微博数据
    func loadStatus(finished: DCZFinishedCallBack) {
        if UserAcountInfo.loadSharedUserAccountInfo()?.access_token == nil {
            print("dingcaozhi----------accessToken 为空")
            return
        }
        let urlString = "2/statuses/home_timeline.json"
        let params : [String : AnyObject] = ["access_token" : UserAcountInfo.loadSharedUserAccountInfo()!.access_token!]
        requestGET(urlString, params: params, finished: finished)
    }
    
    
}
